package cn.xn.hrms.domain.service.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.PersonalSalaryDTO;
import cn.xn.hrms.api.dto.req.PersonalSalaryReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.PersonalSalaryService;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.infrastructure.dao.PersonalSalaryDao;

/**
 * @author ysz
 */
@Service
public class PersonalSalaryServiceImpl implements PersonalSalaryService {

    @Resource
    PersonalSalaryDao personalSalaryDao;
    @Resource
    PersonalService personalService;

    @Override
    public PageInfo<PersonalSalaryDTO> findAll(int year, int month, Integer departmentId, int personalId, int pageNum,
                                               int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PersonalSalaryDTO> personalSalaries = personalSalaryDao.selectAll(year, month,departmentId , personalId);
        return new PageInfo<>(personalSalaries);
    }

    @Override
    public List<PersonalSalaryDTO> all() {
        return personalSalaryDao.selectAll(0, 0, null, 0);
    }

    @Override
    public void insert(PersonalSalaryReqDTO personalSalaryReqDTO) {
        PersonalSalaryDTO personalSalaryDTO = new PersonalSalaryDTO();
        BeanUtils.copyProperties(personalSalaryReqDTO, personalSalaryDTO);
        BigDecimal bigDecimalSum = getBigDecimalSum(personalSalaryReqDTO.getBasisSalary(), personalSalaryReqDTO.getSubsidySalary(),
                personalSalaryReqDTO.getSocialSalary().negate(), personalSalaryReqDTO.getProvidentFund().negate(),
                personalSalaryReqDTO.getBonus());
        personalSalaryDTO.setAllSalary(bigDecimalSum);
        personalSalaryDao.insertSelective(personalSalaryDTO);
    }

    public static BigDecimal getBigDecimalSum(BigDecimal i, BigDecimal... arg) {
        BigDecimal sum = i;
        for (BigDecimal b : arg) {
            sum = sum.add(b);
        }
        return sum;
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        personalSalaryDao.updateIsDeletedByIdIn(1, idList);

    }

    @Override
    public void updateById(Integer id, PersonalSalaryReqDTO personalSalaryReqDTO) {
        PersonalSalaryDTO personalSalaryDTO = personalSalaryDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(personalSalaryDTO)) {
            throw new BaseException(ResultEnum.PERSONAL_SALARY_NOT_EXIST);
        }
        BeanUtils.copyProperties(personalSalaryReqDTO, personalSalaryDTO);
        BigDecimal bigDecimalSum = getBigDecimalSum(personalSalaryReqDTO.getBasisSalary(), personalSalaryReqDTO.getSubsidySalary(),
                personalSalaryReqDTO.getSocialSalary().negate(), personalSalaryReqDTO.getProvidentFund().negate(),
                personalSalaryReqDTO.getBonus());
        personalSalaryDTO.setAllSalary(bigDecimalSum);
        personalSalaryDao.updateByPrimaryKeySelective(personalSalaryDTO);
    }
}
